package stmo.排序算法;

import java.util.Arrays;

/**
 *      冒泡排序
 */

public class Bubble {

    //对数组内的元素进行排序
    public static void sort(Comparable[] a) {
        for(int i = a.length-1;i > 0;i--) {
            for (int j = 0;j < i;j++) {
                if(greater(a[j],a[j+1])) {
                    exch(a,j,j+1);
                }
            }
        }
    }

    //比较
    private static boolean greater(Comparable v,Comparable w) {
        return v.compareTo(w)>0;
    }

    //数据交换
    private static void exch(Comparable[] a,int i,int j) {
        Comparable t;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }


    public static void main(String[] args) {

        // Integer extends Number implements Comparable<Integer>
        Integer[] arr = {3,5,1,7,2,3,4,8};
        Bubble.sort(arr);

        System.out.println(Arrays.toString(arr));
    }

}
